﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DefaultMember : Member
{
    public Text MemberName;
    public Text MemberId;
    /// <summary>
    /// 是否第一次点击
    /// </summary>
    private bool isFirstPressed = true;
    /// <summary>
    /// 是否可以执行检测
    /// </summary>
    private bool checkOK = false;
    /// <summary>
    /// 第一次单击时间
    /// </summary>
    private float firstPressedTime;
    /// <summary>
    /// 第二次单击时间
    /// </summary>
    private float secondPressedTime;
    /// <summary>
    /// 两次单击间隔
    /// </summary>
    private float pressInterval = 0.3f;
    private event Action OnDoubleClick;

    public void Awake()
    {
        GetComponent<Button>().onClick.AddListener(() => {
            if (isFirstPressed)
            {
                firstPressedTime = Time.time;
                isFirstPressed = false;
            }
            else
            {
                secondPressedTime = Time.time;
                isFirstPressed = true;
                checkOK = true;
            }
        });
        OnDoubleClick += () => {
            Debug.Log(Name + "执行双击事件!");
            Singleton<MemberManager>.GetInstance().Remove(Id);  // 移除一个成员
            CtlMsgHandle.GetMemList();      // 重新获取成员列表
        };
    }
    private void Update()
    {
        MemberName.text = Name;
        MemberId.text = Id;
        CheckDoubleClick();
    }
    public void CheckDoubleClick()
    {
        if (checkOK)
        {
            if (secondPressedTime - firstPressedTime <= pressInterval)
                OnDoubleClick();
            checkOK = false;
        }
    }
}
